import {Component, OnInit} from '@angular/core';
import {NavController, Platform} from 'ionic-angular';
import {NewCreatPage} from "../newcreat/newcreat";
import {StoragePage} from "../storage/storage";
import {services} from "../../../providers/service";
import {BarcodeScanner} from "@ionic-native/barcode-scanner";
import {CjStatusPage} from "../cjStatus/cjStatus";
import {AppPopup} from "../../../util/popup";
import {StorageService} from "../../../providers/storage-service";


@Component({
    selector: 'collect',
    templateUrl: 'collect.html',
})
export class CollectPage implements OnInit {
    hasStorage = true;
    myItems = [];
    keyword = "";

    pageNo = 1;
    pageSize = 10;
    count = 0;      //总数

    constructor(public navCtrl: NavController,
                public service: services,
                public barcodeScanner: BarcodeScanner,
                public platform: Platform,
                private popup: AppPopup,
                private storageServ: StorageService,) {

    }

    ngOnInit() {
        this.search();
    }

    ToNewcreat() {
        this.navCtrl.setRoot(NewCreatPage);
    }

    ToStoragePage() {
        this.navCtrl.setRoot(StoragePage);
    }

    //抽检 -- 抽检扫码
    Tcheck(index) {
        console.log(index);
        this.barcodeScanner.scan().then((barcodeData) => {
            if (barcodeData.text == null || barcodeData.text == '') {
                console.log('取消了扫码');
                this.service.isReturn = true;
                return;
            }
            this.service.boxNo = barcodeData.text;
            let param = {
                "boxNo": barcodeData.text,
                "outNo": this.myItems[index].outNo,
                "outId": this.myItems[index].id
            }
            this.storageServ.samplingscan(param)
                .then(res => {
                    console.info("收货记录_抽检扫码成功：", res);
                    this.navCtrl.push(CjStatusPage, {data: res, thisData: this.myItems[index]});
                })
                .catch(err => {
                    console.error("收货记录_抽检扫码失败：", err);
                    this.navCtrl.push(CjStatusPage, {data: err, thisData: this.myItems[index]});
                });
        }, (err) => {
            // An error occurred
            console.error(err);
            if(err = "Illegal access") {
                this.popup.alert("无法打开照相机，请给予相关权限!");
            }

        });


    }

    //下拉刷新
    doRefresh(refresher) {
        console.log('Begin async operation', refresher);
        let param = {
            "keyword": this.keyword
        }
        this.storageServ.receivedList(param, this.pageNo, this.pageSize)
            .then(res => {
                console.info("下拉刷新成功：", res);
                this.myItems = res.infoData.items;
                this.count = Math.ceil(res.infoData.count / this.pageSize);
                refresher.complete();
            })
            .catch(err => {
                console.error("下拉刷新失败：", err);
                refresher.complete();
                this.popup.alert(err.errorMassage);
            });
    }

    //上拉加载更多
    doInfinite(infiniteScroll) {
        if (this.count > this.pageNo) {
            this.pageNo += 1;
            console.log('Begin async operation');
            let param = {
                "keyword": this.keyword
            }
            this.storageServ.receivedList(param, this.pageNo, this.pageSize)
                .then(res => {
                    console.info("上拉加载更多成功：", res);
                    this.myItems = this.myItems.concat(res.infoData.items);
                    this.count = Math.ceil(res.infoData.count / this.pageSize);
                    infiniteScroll.complete();
                })
                .catch(err => {
                    console.error("上拉加载失败：", err);
                    infiniteScroll.complete();
                    this.popup.alert(err.errorMassage);
                });
        } else {
            infiniteScroll.complete();
        }
    }

    search() {
        this.pageNo = 1;
        let param = {
            "keyword": this.keyword
        }
        this.storageServ.receivedList(param, this.pageNo, this.pageSize)
            .then(res => {
                console.info("收货记录_列表查询成功：", res);
                this.myItems = res.infoData.items;
                this.count = Math.ceil(res.infoData.count / this.pageSize);
                if (res.infoData.count == 0) {
                    this.hasStorage = false;
                } else {
                    this.hasStorage = true;
                }
            })
            .catch(err => {
                console.error("收货记录_列表查询失败：", err);
                this.popup.alert(err.errorMassage);
            });
    }

    clearInput() {
        this.keyword = "";
        this.search();
    }
}
